﻿using System;
using UnityEngine;

// Token: 0x020001CB RID: 459
[RequireComponent(typeof(Animator))]
public class MicroLookAtController : MonoBehaviour
{
	// Token: 0x1700014F RID: 335
	// (get) Token: 0x060009BD RID: 2493 RVA: 0x000582BC File Offset: 0x000564BC
	protected Vector3 target
	{
		get
		{
			if (!this.targetEntity)
			{
				return Vector3.zero;
			}
			return (!(this.targetHead != null)) ? (this.targetEntity.transform.position + Vector3.up * this.targetEntity.Height * 0.9f) : this.targetHead.position;
		}
	}

	// Token: 0x060009BE RID: 2494 RVA: 0x00058334 File Offset: 0x00056534
	protected virtual void Awake()
	{
		this.head = base.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Head);
		base.enabled = false;
	}

	// Token: 0x060009BF RID: 2495 RVA: 0x00058350 File Offset: 0x00056550
	public void LookAt(EntityBase entity)
	{
		this.targetEntity = entity;
		if (this.targetEntity != null)
		{
			Animator component = this.targetEntity.gameObject.GetComponent<Animator>();
			this.targetHead = ((!(component != null)) ? null : component.GetBoneTransform(HumanBodyBones.Head));
		}
		this.active = (this.targetEntity != null);
		if (this.prevRot == Quaternion.identity)
		{
			this.prevRot = this.head.rotation;
		}
	}

	// Token: 0x060009C0 RID: 2496 RVA: 0x000583DE File Offset: 0x000565DE
	protected void OnEnable()
	{
		this.prevRot = Quaternion.identity;
	}

	// Token: 0x060009C1 RID: 2497 RVA: 0x000583EC File Offset: 0x000565EC
	protected virtual void LateUpdate()
	{
		if (this.active && this.targetEntity == null)
		{
			this.active = false;
		}
		if (this.active)
		{
			float f = Vector3.Angle(base.transform.forward, this.target - this.head.position);
			if (Mathf.Abs(f) < 85f)
			{
				Quaternion b = Quaternion.LookRotation(this.target - this.head.position);
				this.head.rotation = Quaternion.Slerp(this.prevRot, b, Time.deltaTime * 3f);
			}
			else
			{
				this.head.rotation = Quaternion.Slerp(this.prevRot, this.head.rotation, Time.deltaTime * 3f);
			}
			this.prevRot = this.head.rotation;
		}
		else if (Quaternion.Angle(this.prevRot, this.head.rotation) > 3f)
		{
			this.head.rotation = Quaternion.Slerp(this.prevRot, this.head.rotation, Time.deltaTime * 3f);
			this.prevRot = this.head.rotation;
		}
		else
		{
			base.enabled = false;
		}
	}

	// Token: 0x04000F41 RID: 3905
	protected EntityBase targetEntity;

	// Token: 0x04000F42 RID: 3906
	private Transform targetHead;

	// Token: 0x04000F43 RID: 3907
	protected Transform head;

	// Token: 0x04000F44 RID: 3908
	protected bool active;

	// Token: 0x04000F45 RID: 3909
	protected const float maxLookAngle = 85f;

	// Token: 0x04000F46 RID: 3910
	protected const float headTurnSpeed = 3f;

	// Token: 0x04000F47 RID: 3911
	protected const float minAngleForHeadLerp = 3f;

	// Token: 0x04000F48 RID: 3912
	protected Quaternion prevRot;
}
